<template>
  <div class="app-container">
    <div class="search-container">
      <!-- 数据卡片 start -->
      <el-row :gutter="20">
        <!-- 计费成功数 -->
        <el-col :span="3">
          <el-card shadow="never">
            <div>
              <div class="text-lg text-right">计费成功数</div>
              <div class="text-lg text-right">{{ totalInfo.success }}</div>
              <!-- <svg-icon icon-class="visit" size="2em" /> -->
            </div>
          </el-card>
        </el-col>

        <!-- 计费请求数 -->
        <el-col :span="3">
          <el-card shadow="never">
            <div>
              <div class="text-lg text-right">计费请求数</div>
              <div class="text-lg text-right">
                {{ totalInfo.request }}
              </div>
              <!-- <svg-icon icon-class="ip" size="2em" /> -->
            </div>
          </el-card>
        </el-col>

        <!--计费用户数-->
        <el-col :span="3">
          <el-card shadow="never">
            <div>
              <div class="text-lg text-right">计费用户数</div>
              <div class="text-lg text-right">
                {{ totalInfo.user }}
              </div>
              <!-- <svg-icon icon-class="money" size="2em" /> -->
            </div>
          </el-card>
        </el-col>

        <!--请求转化率-->
        <el-col :span="3">
          <el-card shadow="never">
            <div>
              <div class="text-lg text-right">请求转化率</div>
              <div class="text-lg text-right">
                {{ convertToRate(totalInfo.requestConversionRate) }}%
              </div>
              <!-- <svg-icon icon-class="order" size="2em" /> -->
            </div>
          </el-card>
        </el-col>
        <!--用户转化率-->
        <el-col :span="3">
          <el-card shadow="never">
            <div>
              <div class="text-lg text-right">用户转化率</div>
              <div class="text-lg text-right">
                {{ convertToRate(totalInfo.userConversionRate) }}%
              </div>
              <!-- <svg-icon icon-class="order" size="2em" /> -->
            </div>
          </el-card>
        </el-col>
        <!--升级数量-->
        <el-col :span="3">
          <el-card shadow="never">
            <div>
              <div class="text-lg text-right">升级数量</div>
              <div class="text-lg text-right">
                {{ totalInfo.validate }}
              </div>
              <!-- <svg-icon icon-class="order" size="2em" /> -->
            </div>
          </el-card>
        </el-col>
        <!--成功升级比例-->
        <el-col :span="4">
          <el-card shadow="never">
            <div>
              <div class="text-lg text-right">成功升级比例</div>
              <div class="text-lg text-right">
                {{ totalInfo.successValidateProportion }}
              </div>
              <!-- <svg-icon icon-class="order" size="2em" /> -->
            </div>
          </el-card>
        </el-col>
      </el-row>
      <!-- 数据卡片 end -->
      <el-form
        ref="queryFormRef"
        :model="queryParams"
        :inline="true"
        class="mt-3"
      >
        <el-form-item label="省份" prop="province">
          <el-select
            v-model="queryParams.province"
            placeholder="全部"
            clearable
            class="!w-[100px]"
            filterable
          >
            <el-option
              v-for="item in provinceCode"
              :value="item.name"
              :label="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="bizName" label="业务名称">
          <el-input
            v-model="queryParams.bizName"
            placeholder="业务名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="bizPrice" label="业务价格">
          <el-input
            v-model="queryParams.bizPrice"
            placeholder="业务价格"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="bizID" label="业务id">
          <el-input
            v-model="queryParams.bizID"
            placeholder="业务id"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="bizNo" label="渠道号">
          <el-input
            v-model="queryParams.bizNo"
            placeholder="渠道号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="bizType" label="业务类型">
          <el-input
            v-model="queryParams.bizType"
            placeholder="业务类型"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="时间范围">
          <el-date-picker
            v-model="searchTime"
            type="datetimerange"
            unlink-panels
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            @change="handleChangeTime"
            :default-time="defaultTime"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetQuery"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="conversionRateList"
        highlight-current-row
        border
        stripe
      >
        <el-table-column
          label="序号"
          type="index"
          width="60"
          :index="indexMethod"
        />
        <el-table-column label="业务名称" width="110" prop="bizName" sortable />
        <el-table-column label="业务价格" width="100" prop="bizPrice" />
        <el-table-column label="业务id" min-width="100" prop="bizID" />
        <el-table-column label="业务类型" width="110" prop="bizType" sortable />
        <el-table-column label="渠道号" width="100" prop="bizNo" sortable />
        <el-table-column
          label="计费成功数"
          align="center"
          prop="success"
          width="120"
          sortable
        />
        <el-table-column
          label="计费请求数"
          prop="request"
          width="120"
          sortable
        />
        <el-table-column
          label="计费用户数"
          align="center"
          prop="user"
          width="120"
          sortable
        />
        <el-table-column
          label="请求转化率"
          align="center"
          prop="requestConversionRate"
          width="120"
          sortable
        >
          <template #default="scope">
            {{ convertToRate(scope.row.requestConversionRate) }}%
          </template>
        </el-table-column>
        <el-table-column
          label="用户转化率"
          align="center"
          prop="userConversionRate"
          width="120"
          sortable
        >
          <template #default="scope">
            {{ convertToRate(scope.row.userConversionRate) }}%
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.size"
        @pagination="handleQuery"
      />
    </el-card>
  </div>
</template>
<script setup lang="ts">
import { getConversionRatePage, getTotal, getHour } from "@/api/conversionRate";
import {
  ConversionRateQuery,
  ConversionRatePageVO,
} from "@/api/conversionRate/types";
import { provinceCode } from "@/utils/constant";
import { convertToRate } from "@/utils/index";
defineOptions({
  name: "Role",
  inheritAttrs: false,
});

const queryFormRef = ref(ElForm);

const loading = ref(false);
const total = ref(0);
const defaultTime: [Date, Date] = [
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59),
]; // '12:00:00', '08:00:00'
const indexMethod = (index: number) => {
  return index + 1;
};
const searchTime = ref(null);
const totalInfo = reactive({
  success: 0,
  request: 0,
  user: 0,
  requestConversionRate: 0,
  userConversionRate: 0,
});
const queryParams = reactive<ConversionRateQuery>({
  page: 1,
  size: 10,
});

const conversionRateList = ref<ConversionRatePageVO[]>();

function initSearchTime() {
  const start = new Date(
    new Date(
      `${new Date().getFullYear()}-${new Date().getMonth() + 1}-${new Date().getDate()} 00:00:00`
    ).getTime() -
      3600 * 1000 * 24 * 31
  );
  const end = new Date(
    `${new Date().getFullYear()}-${new Date().getMonth() + 1}-${new Date().getDate()} 23:59:59`
  );
  searchTime.value = [start, end];
  console.log("🚀 ~ initSearchTime ~ searchTime:", searchTime);
  queryParams.dateStart = new Date(
    start.getTime() + 8 * 3600 * 1000
  ).toISOString();
  queryParams.dateEnd = new Date(end.getTime() + 8 * 3600 * 1000).toISOString();
}
function handleQueryTotal() {
  getTotal().then((data) => {
    console.log("🚀 ~ .then ~ data:", data);
    totalInfo.success = data.success;
    totalInfo.request = data.request;
    totalInfo.user = data.user;
    totalInfo.requestConversionRate = data.requestConversionRate;
    totalInfo.userConversionRate = data.userConversionRate;
    totalInfo.validate = data.validate;
    totalInfo.successValidateProportion = data.successValidateProportion;
  });
}
/** 查询 */
function handleQuery() {
  loading.value = true;
  if (queryParams.province === "全国") {
    queryParams.province = "";
  }
  getConversionRatePage(queryParams)
    .then((data) => {
      console.log("🚀 ~ .then ~ data:", data);
      conversionRateList.value = data.rows;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}
/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.page = 1;
  initSearchTime();
  handleQueryTotal();
  handleQuery();
}

function handleChangeTime(val) {
  if (!val) {
    return;
  }
  console.log("🚀 ~ handleChangeTime ~ val:", val);
  const [start, end] = val;
  queryParams.dateStart = new Date(
    start.getTime() + 8 * 3600 * 1000
  ).toISOString();
  queryParams.dateEnd = new Date(end.getTime() + 8 * 3600 * 1000).toISOString();
}
onMounted(() => {
  initSearchTime();
  handleQueryTotal();
  handleQuery();
});
</script>
